/*
 * 上海远境文化传媒有限公司 版权所有
 */
package facade.vo.base;

import models.base.AdminUser;
import models.base.AdminUserRole;
import models.base.Role;
import models.constants.DeletedStatus;
import models.enums.AvailableStatus;
import play.Logger;

import java.io.Serializable;
import java.util.Set;

/**
 * 用户值对象.
 */
public class LoginUserVO implements Serializable {

    private static final long serialVersionUID = -8908366898420430708L;

    public String id;

    public String name;

    public String loginName;

    public String mobile;

    public String corpId;

    public String corpName;

    public String mechanismId;

    public String mechanismName;

    public String storeId;

    public String storeName;

    public String roleId;

    public Set<String> roleSet;



    public static LoginUserVO from(AdminUser user) {
        if (user.deleted == DeletedStatus.DELETED || user.status == AvailableStatus.FREEZE) {
            Logger.warn("LOG06033: 用户已经被删除，跳过：" + user);
            return null;
        }
        LoginUserVO vo = new LoginUserVO();

        if (user.corp != null) {
            vo.corpId = user.corp.getId();
            vo.corpName = user.corp.name;
        }

        vo.id = user.getId();
        vo.name = user.name;
        vo.loginName = user.loginName;
        vo.mobile = user.mobile;
        AdminUserRole role = AdminUserRole.findByUserId(user.getId());
        if(role != null){
            vo.roleId = role.role.getId();
        }

        vo.roleSet = Role.loadRoles(user);
        return vo;
    }

}
